package org.itboys.mobile.service.mongo.mobile.count;

import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.count.MdlCountDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.mobile.cart.MdlMobileCartService;
import org.itboys.mobile.service.mongo.mobile.member.SundayMobileMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/5
 * 描述：迈迪龙_手机_结算
 */
@Service
public class MdlMobileCountService {
    @Autowired
    private MdlMobileCartService cartService;
    @Autowired
    private SundayMobileMemberService memberService;

    /**
     * 购物车结算。双版本通用
     * @param memberId
     * @param cartIds
     * @param cartType
     * @return
     */
    public MdlCountDto countCart(Long memberId, List<Long> cartIds,  Integer cartType,Integer sendType){
        MdlCountDto countDto = cartService.getCart(memberId,cartIds,cartType);
        SundayMember member=memberService.getById(memberId);
        if(member.getUserType()== SundayCommonConstants.USER_TYPE_AGENT){
            countDto.setDiscount(new BigDecimal(member.getDiscount().doubleValue()/100).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        BigDecimal totalAmount = countDto.getTotalAmount();
        BigDecimal payAmount = countDto.getPayAmount();


        if(countDto.getType()==SundayCommonConstants.type_mall){
            countDto.setSendType(sendType);
            //Map<String,Object> result = expressModeService.matching(payAmount.doubleValue());
            //待补充运费信息
            Map<String,Object> result=new HashMap<String,Object>();
            BigDecimal sendAmount =new BigDecimal("13");
            String sendMsg= (String)result.get("sendMsg");
            totalAmount=totalAmount.add(sendAmount);
            payAmount=payAmount.add(sendAmount);
            countDto.setSendAmount(sendAmount);
            countDto.setSendMsg(sendMsg);
        }else if(countDto.getType()==SundayCommonConstants.type_caigou){
            payAmount=payAmount.multiply(countDto.getDiscount());
            countDto.setSendType(sendType);
            //待补充运费信息
            Map<String,Object> result=new HashMap<String,Object>();
            BigDecimal sendAmount =new BigDecimal("12");
            String sendMsg= (String)result.get("sendMsg");
            totalAmount=totalAmount.add(sendAmount);
            payAmount=payAmount.add(sendAmount);
            countDto.setSendAmount(sendAmount);
            countDto.setSendMsg(sendMsg);
        }
        countDto.setTotalAmount(totalAmount);
        countDto.setPayAmount(payAmount);
        return countDto;
    }

    /**
     * 立即购买结算
     * @param memberId
     * @param productId
     * @param number
     * @return
     */
    public MdlCountDto countBuyNow(Long memberId,Long productId,Integer number,Integer sendType){
        List<Long> productIds =new ArrayList<Long>();
        productIds.add(productId);
        List<Integer> numbers =new ArrayList<Integer>();
        numbers.add(number);
        List<Long> cartIds= cartService.addCart(memberId,productIds ,numbers, SundayCommonConstants.cart_type_buy_now);
        return countCart(memberId,cartIds,SundayCommonConstants.cart_type_buy_now,sendType);
    }
}
